<template>
  <div class="no-login" v-if="isToken === false">
    <van-button round type="info" size="normal" @click="wxLoginOptional()"
      >微信一键登录</van-button
    >
  </div>
  <div v-else>
    <van-cell
      title="昵称"
      is-link
      :value="wxUserInfo.username"
      @click="gotoPage('用户名', 'username', wxUserInfo.username)"
    ></van-cell>
    <van-cell title="头像" is-link @click="gotoPage('头像','avatarUrl',wxUserInfo.avatarUrl)">
      <template >
        <van-image
          width="50"
          height="50"
           fit="cover"
          :src="wxUserInfo.avatarUrl"
        />
      </template>
    </van-cell>
    <van-cell
      title="性别"
      is-link
      :value="wxUserInfo.gender === 0 ? '男' : '女'"
      @click="gotoPage('性别', 'gender', wxUserInfo.gender)"
    ></van-cell>
    <van-cell
      title="邮箱"
      is-link
      :value="userInfo.email"
      @click="gotoPage('邮箱', 'email', wxUserInfo.email)"
    ></van-cell>
    <van-cell
      title="个人标签"
      is-link
      :value="userInfo.email"
      @click="toUserTags()"
    ></van-cell>
    <van-cell title="已加入的队伍" is-link @click="toHasJoinTeam()" />
    <van-cell title="已创建的队伍" is-link @click="toHasCreateTeam()" />
    <!-- <van-button @click="getTest">测试请求</van-button> -->
  </div>
  <van-notify id="van-notify" />
</template>

<script setup>

import { test } from '../../api/user/testApi.js'
import { wxLogin,getCurrentUser } from '../../api/user/userApi.js'
import { onMounted, ref, reactive } from 'vue'
import useStore from '../../store';
import { onActivated , inject } from 'vue';
import { onShow } from '@dcloudio/uni-app';



import WebsocketUtil from '../../utils/webSocket'; 

// import {useUserModule} from '../store/modules/user'
const isToken = ref(false) //是否存在token，默认不登录，即没有token
const userInfo = ref({
  id: 0,
  username: '',
  userAccount: '',
  gender: 0,
  phone: '',
  email: '',
  userStatus: 0,
  userRole: 0,
  planetCode: '',
  tags: '',
  createTime: new Date(),
})
const wxUserInfo=ref({})
const wxLoginDTO = reactive({
  code: '',
})
const webSocket=ref(null)

/**
 *
 * @param eidtName 编辑名，例如编辑用户名，编辑名为用户名
 * @param keyWord 关键字，例如用户名，关键字为username
 * @param params 参数
 */
const gotoPage = (editName, keyWord, params) => {
  console.log("params",params);
  
  useStore().user().setEditUser(editName,keyWord,params)
  uni.navigateTo({ url: '/pagePerson/user/editUserInfo' })
  // useUserModule().setEditUserInfo({editName,keyWord,params});
}
const toHasJoinTeam = () => {
  uni.navigateTo({ url: '/pageTeam/hasJoinTeam' })
}
const toHasCreateTeam = () => {
  uni.navigateTo({ url: '/pageTeam/hasCreatedTeam' })
}
const getTest = async () => {
  // const res=await test()
  // console.log(res);
  uni.login({
    provider: 'weixin', //使用微信登录
    success: function (loginRes) {
      console.log(loginRes)
    },
  })
}
//获取登录状态
const getTokenStatus = () => {
  const Authorization = uni.getStorageSync('Authorization')
  console.log(Authorization)

  if (Authorization === null) {
    isToken.value = false
  } else {
    isToken.value = true
  }
}
//微信登录操作
const wxLoginOptional = () => {
    uni.login({
    provider: 'weixin', //使用微信登录
    success:(loginRes) =>{
      console.log(loginRes.code)
      wxLoginDTO.code=loginRes.code
      console.log(wxLoginDTO.code)
      
    },
  })
  uni.getUserProfile({
    desc:"获取用户信息",
    success:async (userRes)=>{
        console.log(res);
        const res=await wxLogin(wxLoginDTO)
        if(res.code===0){
            uni.setStorageSync("Authorization",res.data.token)
            getTokenStatus()
            //使用websocketutil工具类发送消息
            const wsProdUrl='wss://www.pickteams.cn/api/ws/'
            const wsDevUrl='ws://localhost:8689/api/ws/'
            webSocket.value=new WebsocketUtil(wsProdUrl+res.data.id,5)
            
        }else{
            uni.showModel({
                title:'请求失败'
            })
        }
        getUserProfile()
    }
  })

}
const getUserProfile=async ()=>{
 const data= await useStore().user().getCurrentUserInfo()
  console.log(data);
  wxUserInfo.value=data
  useStore().user().judgeFlash(false)
  
}
//转个人标签页
const toUserTags=()=>{
  uni.navigateTo({url:'/pagePerson/user/userTags'})
}
//上传头像
const uploadAvatar=()=>{
  
}
onShow(()=>{
  console.log(useStore().user().IS_FLASH);
  
  if(useStore().user().isFlash){
    getUserProfile()
  }
  console.log(1111111111111+"==================");
  
})
onMounted(() => {
  getTokenStatus()
  
  
})
</script>
<style scoped>
.no-login {
  margin: 0 auto;
  text-align: center;
  height: 100vh;
  line-height: 100vh;
}
</style>
